package sol.leetcode;

/**
 * Maximum Subarray
 */
public class Issue053 {
    public int maxSubArray(int[] nums) {
        int sum = nums[0], maxSum = nums[0];
        for (int i = 1; i < nums.length; i++) {
            if (sum < 0) sum = 0;   //先判断之前的sum能否被这次利用（小于0则抛弃）
            sum += nums[i];
            maxSum = Math.max(maxSum, sum);
        }
        return maxSum;
    }

    public static void main(String[] args) {
        int[] nums = {-2, 1, -3, 4, -1, 2, 1, -5, 4};
        System.out.println(new Issue053().maxSubArray(nums));
    }

}
